{
    abstract class Component {
        protected name: string
        constructor(name: string) {
            this.name = name
        }
        public abstract doOperation(): void
        public add(component: Component): void { }
        public remove(component: Component): void { }
        public getComponentList(): Array<Component> {
            return new Array<Component>()
        }
    }

    class Composite extends Component {
        private componentList: Array<Component>

        constructor(name: string) {
            super(name)
            this.componentList = new Array<Component>
        }

        public doOperation(): void {
            console.log(`这是容器 ${this.name},负责业务逻辑`);
        }

        public add(component: Component): void {
            this.componentList.push(component)
        }

        public remove(component: Component): void {
            const componentIndex = this.componentList.findIndex((value: Component) => {
                return value === component
            })
            this.componentList.splice(componentIndex, 1)
        }

        public getComponentList(): Component[] {
            return this.componentList
        }

    }

    class SpecificComponent extends Component {
        constructor(name: string) {
            super(name)
        }

        public doOperation(): void {
            console.log("这是具体零件，负责具体业务逻辑");
        }
    }

    const CompositionTest = (): void => {
        let baseComponent1: Composite = new Composite("基础零件1")
        let addComponent1: SpecificComponent = new SpecificComponent("添加零件1")

        let baseComponent2: Composite = new Composite("基础零件2")
        let addComponent2: SpecificComponent = new SpecificComponent("添加零件2")

        //组合各个零件
        baseComponent1.add(addComponent1)
        baseComponent1.add(baseComponent2)
        baseComponent1.add(addComponent2)

        console.log('before add baseComponent2_1 component list :>> ', baseComponent1.getComponentList());
        const baseComponent2_1: Component = new SpecificComponent("添加在基础零件2上面的零件")
        baseComponent2.add(baseComponent2_1)
        console.log('baseComponent1 component list :>> ', baseComponent1.getComponentList());
        baseComponent1.remove(addComponent1)
        console.log('after remove baseComponent1 component list :>> ', baseComponent1.getComponentList());
    }


    CompositionTest()
}
